//
//  MenuViewController.m
//  BulgeFramework
//
//  Created by beisheng03 on 2018/2/23.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "MenuViewController.h"
#import "BaseDisablelightBtn.h"
#import "TestController.h"
@interface MenuViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
/** 上一次按钮 */
@property (nonatomic, weak) UIButton *selectButton;

@property (nonatomic, strong) NSMutableArray *titleButtons;

/** 下划线 */
@property (nonatomic, weak) UIView *underline;
@end


#define XMGColor(r , g ,b) [UIColor colorWithRed:(r)  green:(g)  blue:(b) alpha:1]
#define XMGScreenW [UIScreen mainScreen].bounds.size.width
#define XMGScreenH [UIScreen mainScreen].bounds.size.height
@implementation MenuViewController


//fdsfd
- (NSMutableArray *)titleButtons
{
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的红包";
    
    // 2.搭建内容滚动视图
    [self setupContentScrollView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (@available(iOS 11.0, *)) {
        
        
        self.contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    }
    
    // 1.搭建标题滚动视图
    [self setupTitleScrollView];
    
    
    // 3.添加子控制器
    [self setupAllChildViewController];
    
    // 取消添加额外滚动区域
    // 在scrollView,忽然发现内容放下走,就是顶部额外滚动区域做
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    // 5.监听滚动完成
    self.contentScrollView.delegate = self;
    
    
    [self setupAllTitle];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}



#pragma mark - 搭建标题滚动视图
- (void)setupTitleScrollView
{
    // 创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor whiteColor];
    //    [[UIColor whiteColor] colorWithAlphaComponent:0.9]
    CGFloat y = IPhoneX ? 88 : 64;
    CGFloat h = XMGTitlesViewH;
    CGFloat w = XMGScreenW;
    scrollView.frame = CGRectMake(0, y, w, h);
    [self.view addSubview:scrollView];
    _titleScrollView = scrollView;
}


#pragma mark - 搭建内容滚动视图
- (void)setupContentScrollView
{
    // 创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    
    scrollView.backgroundColor = [UIColor whiteColor];
    //    CGFloat y = CGRectGetMaxY(_titleScrollView.frame);
    //    CGFloat h = XMGScreenH - y;
    //    CGFloat w = XMGScreenW;
    //    scrollView.frame = CGRectMake(0, y, w, h);
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    _contentScrollView = scrollView;
    
}




#pragma mark -添加子控制器
- (void)setupAllChildViewController
{
    
    
    TestController *first = [[TestController alloc] init];
    first.title = @"oneVC";
    [self addChildViewController:first];
    
    TestController *secend = [[TestController alloc] init];
    secend.title = @"twoVC";
    [self addChildViewController:secend];
    
    
    
}

#pragma mark -设置标题
- (void)setupAllTitle
{
    // 添加所有标题
    NSUInteger count = self.childViewControllers.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 72 > (KScreenW / count) ? 72 : (KScreenW / count);
    
    CGFloat h = self.titleScrollView.bounds.size.height;
    
    for (NSUInteger i = 0; i < count; i++) {
        x = i * w;
        UIViewController *vc = self.childViewControllers[i];
        BaseDisablelightBtn *btn = [BaseDisablelightBtn buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = MYFONT(15);
        btn.tag = i;
        btn.frame = CGRectMake(x, y, w, h);
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchDown];
        [self.titleScrollView addSubview:btn];
        
        // 添加到标题按钮数组
        [self.titleButtons addObject:btn];
        
    }
    
    
    // 设置标题滚动范围
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
    // 设置内容滚动范围
    self.contentScrollView.contentSize = CGSizeMake(count * XMGScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    /*
     问题:1.按钮文字为黑色 2.标题滚动视图可以滚动
     */
    [self titleClick:self.titleButtons[self.seletIndex]];
    
    [self setupLine];
    
    // 下划线
    [self setupUnderline];
}



- (void)setupLine{
    
    // 标题按钮
    //    ZMTabBarButton *firstTitleButton = self.titleScrollView.subviews.firstObject;
    
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 0.5;
    underline.frame = CGRectMake(0, self.titleScrollView.height - underlineH, self.titleScrollView.contentSize.width, underlineH);
    underline.backgroundColor = colorWithA(75, 75, 75,0.2);
    [self.titleScrollView addSubview:underline];
    
    
}



/**
 *  下划线
 */
- (void)setupUnderline
{
    // 标题按钮
    BaseDisablelightBtn *firstTitleButton = self.titleScrollView.subviews[self.seletIndex];
    
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 2;
    underline.frame = CGRectMake(0, self.titleScrollView.height - underlineH, 0, underlineH);
    underline.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    [self.titleScrollView addSubview:underline];
    self.underline = underline;
    
    // 默认选中最前面的按钮
    // 切换按钮状态
    firstTitleButton.selected = YES;
    //    self.selectButton = firstTitleButton;
    
    // 处理下划线
    [firstTitleButton.titleLabel sizeToFit];
        self.underline.width = KScreenW/self.childViewControllers.count;
//    self.underline.width = 90;
    
    
    self.underline.centerX = firstTitleButton.centerX;
}

#pragma mark -点击标题就会调用
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.选中按钮
    [self selectBtn:button];
    
    // 2.需要把对应子控制器的view添加上去
    [self setupOneViewControllerView:i];
    
    // 3.设置内容滚动视图偏移量
    // 计算偏移量
    CGFloat offsetX = i * XMGScreenW;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
}




#pragma mark -选中按钮
- (void)selectBtn:(UIButton *)selectBtn
{
    // 设置按钮颜色
    [_selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [selectBtn setTitleColor:themeColor forState:UIControlStateNormal];
    
    // 让标题居中
    [self setupTitleButtonCenter:selectBtn];
    
    // 标题缩放
    _selectButton.transform = CGAffineTransformIdentity;
        selectBtn.transform = CGAffineTransformMakeScale(1.3, 1.3);
    
    _selectButton = selectBtn;
    
    
    [UIView animateWithDuration:0.5 animations:^{
        // 处理下划线
        self.underline.width = KScreenW/self.childViewControllers.count;
//        self.underline.width = 90;
        self.underline.centerX = selectBtn.centerX;
        
    } completion:^(BOOL finished) {
        // 2.需要把对应子控制器的view添加上去
//        [self setupOneViewControllerView:selectBtn.tag];
    }];
}

// 让标题居中
- (void)setupTitleButtonCenter:(UIButton *)button
{
    // 修改偏移量
    CGFloat offsetX = button.center.x - XMGScreenW * 0.5;
    
    // 处理最小滚动偏移量
    if (offsetX < 0) {
        offsetX = 0;
    }
    
    // 处理最大滚动偏移量
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - XMGScreenW;
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

// 添加一个子控制器的view
- (void)setupOneViewControllerView:(NSInteger)i
{
    CGFloat x = i * XMGScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    if (vc.view.superview) return;
    vc.view.frame = CGRectMake(x, 0, XMGScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
    
}
#pragma mark -UIScrollViewDelegate
// 减速完成就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / XMGScreenW;
    
    // 获取对应标题按钮
    UIButton *selButton = self.titleButtons[i];
    
    // 选中标题
    [self selectBtn:selButton];
    
    // 添加对应子控制器的view
    [self setupOneViewControllerView:i];
}

// 只要滚动 就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 让按钮形变
    NSInteger leftI = scrollView.contentOffset.x / XMGScreenW;
    
    NSInteger rightI = leftI + 1;
    
    // 1.获取需要形变的按钮
    
    // left
    // 获取左边按钮
    UIButton *leftButton = self.titleButtons[leftI];
    
    // right
    NSUInteger count = self.childViewControllers.count;
    UIButton *rigthButton;
    // 获取右边按钮
    if (rightI < count) {
        rigthButton = self.titleButtons[rightI];
    }
    
    // 计算右边按钮偏移量
    CGFloat rightScale = scrollView.contentOffset.x / XMGScreenW;
    
    // 只想要 0~1
    rightScale = rightScale - leftI;
    
    
    CGFloat leftScale = 1 - rightScale;
    
    
    CGFloat rightBtnCenterX = rigthButton.centerX;
    CGFloat leftBtnCenterX = leftButton.centerX;
    
    
    CGFloat subtract = fabs(rightBtnCenterX - leftBtnCenterX);
    
    CGFloat temp = leftButton.centerX + rightScale * subtract;
    
    self.underline.centerX = temp;
    
    // 形变按钮
    // scale 0 ~ 1 => 1 ~ 1.3
    leftButton.transform = CGAffineTransformMakeScale(leftScale * 0.3 + 1, leftScale * 0.3 + 1);
    rigthButton.transform = CGAffineTransformMakeScale(rightScale * 0.3 + 1, rightScale * 0.3 + 1);
    //    themeColor
    // 颜色渐变 黑色 -> 红色
    //  XMGColor(rightScale, 0, 0)
    //    XMGColor(leftScale, 0, 0)
    [rigthButton setTitleColor:colorWith(23* rightScale, 77 * rightScale, 237 * rightScale) forState:UIControlStateNormal];
    [leftButton setTitleColor:colorWith(23 * leftScale, 77 * leftScale, 237 * leftScale) forState:UIControlStateNormal];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    
    
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
